package com.zhaosc.elasticsearch;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.Test;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-document-get.html
 * @author zhaosc
 *
 */
public class GetTest extends BaseTest {

	private static final Logger logger = Logger.getLogger(IndexTest.class);

	@Test
	public void testGet01() {

		GetRequest getRequest = new GetRequest(movies, "3843");
		getRequest.fetchSourceContext(FetchSourceContext.FETCH_SOURCE);
		// getRequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
		try {
			GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
			//String message = (String) getResponse.getSourceAsMap().get("message");
			logger.info("****"+getResponse.getSourceAsString());
			//logger.info("*****message*****" + message);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Test
	public void testGet02() {
		GetRequest getRequest = new GetRequest(index, "1");
		String[] includes = new String[] { "message", "*Date" };
		String[] excludes = new String[] {};
		FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
		getRequest.fetchSourceContext(fetchSourceContext);
		try {
			GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
			String message = (String) getResponse.getSourceAsMap().get("message");
			logger.info("*****message*****" + message);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Test
	public void testGet03() {
		GetRequest getRequest = new GetRequest(index, "1");
		String[] includes = new String[] {};
		String[] excludes = new String[] { "message" };

		FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
		getRequest.fetchSourceContext(fetchSourceContext);
		getRequest.storedFields("message");
		try {
			GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
			String message = getResponse.getField("message").getValue();
			logger.info("*****message*****" + message);
		} catch (IOException e) {
			logger.error(e);
		}
	}

}
